library IEEE;
use IEEE.STD_LOGIC_1164.all;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity taxi is
PORT(
sys_clk : IN STD_LOGIC;--50M系统时钟输入
rst_cus : IN STD_LOGIC;--上客信号输入
rst_rid : IN STD_LOGIC;--行驶信号输入

LEDrun,LEDwait,point: OUT STD_LOGIC;
screen_led1,screen_led2,screen_led3,screen_led4:out STD_LOGIC_VECTOR(7 DOWNTO 0);

num1,num2,num3,nump,numz:out STD_LOGIC_VECTOR(3 DOWNTO 0));--LED灯闪动信号输出
END taxi;
ARCHITECTURE Behavioral OF taxi IS
SIGNAL screen_flag1,screen_flag2,screen_flag3,LAST,zero: STD_LOGIC_VECTOR(3 DOWNTO 0);--延时计数器，对50M时钟分频
SIGNAL led_delay : STD_LOGIC_VECTOR(3 DOWNTO 0);--延时计数器，对50M时钟分频
SIGNAL THREE : STD_LOGIC;
SIGNAL flag1,flag2,flip,up,star: STD_LOGIC;--亮灯标识

BEGIN
PROCESS(sys_clk,rst_cus,rst_rid)
BEGIN

IF rst_cus = '0' THEN--复位，对led_delay请零

		screen_flag1(3)<='0';
		screen_flag1(2)<='0';
		screen_flag1(1)<='1';
		screen_flag1(0)<='1';
		screen_flag2(3)<='0';
		screen_flag2(2)<='0';
		screen_flag2(1)<='0';
		screen_flag2(0)<='0';
		up<='0';
	IF star='0' THEN 
		up<='0';
		flip<='0';
	END IF;
led_delay <= (OTHERS => '0');
ELSIF sys_clk'EVENT AND sys_clk = '1' THEN--时钟上升沿，led_delay加一
		led_delay <= led_delay + '1';--行驶中
		
			IF rst_rid = '1' THEN--行驶状态
				flag1<=led_delay(2);--行驶灯
				flag2<='0';
				
				IF led_delay(2)='1' and flip = '0' THEN--分频后
					screen_flag1<=screen_flag1+'1';
					flip<='1';
					IF screen_flag1(3)='1' and screen_flag1(0)='1' THEN--进位临界+四位清零
						screen_flag1(3)<='0';
						screen_flag1(2)<='0';
						screen_flag1(1)<='0';
						screen_flag1(0)<='0';
						screen_flag2<=screen_flag2+'1';
					END IF;
				END IF;
				IF led_delay(2)='0' THEN--flip翻转
				flip<='0';
				END IF;
				ElSE--等候状态
				flag2<=led_delay(2);--等候灯
				flag1<='0';
				IF led_delay(2)='1' and flip = '0' THEN--分频后
					screen_flag3<=screen_flag3+'1';
					flip<='1';
					IF screen_flag3(3)='1' and screen_flag3(0)='1' THEN--进位临界+四位清零
						screen_flag3(3)<='0';
						screen_flag3(2)<='0';
						screen_flag3(1)<='0';
						screen_flag3(0)<='0';
						IF up ='1' THEN 
							screen_flag1<=screen_flag1+'1';
							IF screen_flag1(3)='1' and screen_flag1(0)='1' THEN--进位临界+四位清零
								screen_flag1(3)<='0';
								screen_flag1(2)<='0';
								screen_flag1(1)<='0';
								screen_flag1(0)<='0';
								screen_flag2<=screen_flag2+'1';
							END IF;
							up<='0';
						ELSE
							up<='1';
						END IF;
					END IF;
				END IF;
				IF led_delay(2)='0' THEN--flip翻转
					flip<='0';
				END IF;
			END IF;
END IF;
IF up='1' THEN
	LAST(3)<='0';	
	LAST(2)<='1';
	LAST(1)<='0';
	LAST(0)<='1';
ElSE
	LAST(3)<='0';	
	LAST(2)<='0';
	LAST(1)<='0';
	LAST(0)<='0';
END IF;
zero(3)<='0';	
zero(2)<='0';	
zero(1)<='0';	
zero(0)<='0';	
END PROCESS;
PROCESS(screen_flag1,screen_flag2,LAST,rst_cus)
begin

case screen_flag1 is--个位
   when "0000"=>screen_led1<="01000000" ;
   when "0001"=>screen_led1<="01111001" ;
   when "0010"=>screen_led1<="00100100" ;
   when "0011"=>screen_led1<="00110000" ;
   when "0100"=>screen_led1<="00011001" ;
   when "0101"=>screen_led1<="00010010" ;
   when "0110"=>screen_led1<="00000010" ;
   when "0111"=>screen_led1<="01111000" ;
   when "1000"=>screen_led1<="00000000" ;
   when "1001"=>screen_led1<="00010000" ;
   when others=>null;
end case;
case screen_flag2 is--十位
   when "0000"=>screen_led2<="11000000" ;
   when "0001"=>screen_led2<="11111001" ;
   when "0010"=>screen_led2<="10100100" ;
   when "0011"=>screen_led2<="10110000" ;
   when "0100"=>screen_led2<="10011001" ;
   when "0101"=>screen_led2<="10010010" ;
   when "0110"=>screen_led2<="10000010" ;
   when "0111"=>screen_led2<="11111000" ;
   when "1000"=>screen_led2<="10000000" ;
   when "1001"=>screen_led2<="10010000" ;
   when others=>null;
end case;
case LAST is--十分位
   when "0000"=>screen_led3<="11000000" ;
   when "0001"=>screen_led3<="11111001" ;
   when "0010"=>screen_led3<="10100100" ;
   when "0011"=>screen_led3<="10110000" ;
   when "0100"=>screen_led3<="10011001" ;
   when "0101"=>screen_led3<="10010010" ;
   when "0110"=>screen_led3<="10000010" ;
   when "0111"=>screen_led3<="11111000" ;
   when "1000"=>screen_led3<="10000000" ;
   when "1001"=>screen_led3<="10010000" ;
   when others=>null;
end case;
screen_led4<="11000000" ; 
END PROCESS;
LEDrun <= flag1;--分频器的高位作为LED灯闪动信号输出
LEDwait <= flag2;
num1<=screen_flag1;--个位
num2<=screen_flag2;--十位
num3<=screen_flag3;
numz<=zero;--百分位
nump<=LAST;--十分位

end Behavioral;